package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/manuscriptrpc/manuscriptrpc"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);not null;comment:'标题'"`
	Content string `gorm:"type:varchar(100);not null;comment:'内容'"`
	ClassId int64  `gorm:"type:int(11);not null;comment:'类型编号'"`
}

func (m *Manuscript) Manuscript(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Manuscript) FindManuscriptByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(m).Error
}

func ManuscriptList(db *gorm.DB) ([]*manuscriptrpc.ManuscriptFind, error) {
	var list []*manuscriptrpc.ManuscriptFind
	err2 := db.Debug().Model(&Manuscript{}).Joins("LEFT JOIN classes ON manuscripts.class_id = classes.id").Select(
		"manuscripts.id",
		"manuscripts.title",
		"manuscripts.content",
		"classes.class_name",
	).Find(&list).Error
	return list, err2
}
